﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using VietPublisher.BaseProject.Utils.Miscellaneous;



namespace VietPublisher.BaseProject.Models.cart
{
    /// <summary>
    /// This is a cart .Display UserName , Quantity , Total and items
    /// </summary>
    public class Cart
    {



        public Cart() {
            EmptyItems();
        }
        public Cart(CartItem cartItem)
        {            
            AddToCart(cartItem);
        }

        public String UserName {
            get
            {
                return HttpContext.Current.Session[DConst.userName] == null ? string.Empty : HttpContext.Current.Session[DConst.userName].ToString();
            }
            set {
                if (!String.IsNullOrEmpty(value))
                {
                    HttpContext.Current.Session[DConst.userName] = value;
                }
            }
      
        }             

        /// <summary>
        /// Total quantity of the cart
        /// </summary>
        public int Quantity { get; set; }

        public List<CartItem> items { get; set; }        

        /// <summary>
        /// Total Pricing of the cart
        /// </summary>
        public double Total { get; set; }

        public bool isBookExists(int bookID) {
            bool isExist = false;
            foreach (var item in items)
            {
                if (item.BookID == bookID) {
                    isExist = true;
                    break;
                }
            }
            return isExist;
        }

        /// <summary>
        /// Add one Item to cart
        /// </summary>
        /// <param name="itemName"></param>
        /// <param name="quantity"></param>
        /// <param name="price"></param>
        public Cart AddToCart(CartItem cartItem)
        {
            //CartItem cartItem = new CartItem(bookID , bookCode,itemName, quantity,price);
            if (items == null) {
                items = new List<CartItem>();
            }

            if (!isBookExists(cartItem.BookID))
            {                      
                this.Quantity += cartItem.Quantity;//set total quantity                
                cartItem.Total = cartItem.Quantity * cartItem.Price;
                this.Total += cartItem.Total;// set total Pricing           
                items.Add(cartItem);          
            }
            else {
                foreach (var item in items)
                {
                    if (item.BookID == cartItem.BookID) {
                        item.UpdateQuantity(item.Quantity+1);                             
                        this.Quantity++;
                        item.Total = item.Quantity * item.Price;
                        this.Total += item.Total;                        
                        break;
                    }
                }
            }
            return this;         
        }

        public CartItem GetCartItemFromBookID(int bookID) {
            CartItem cartItem = null;
            foreach (var item in items)
            {
                if (item.BookID == bookID) {
                    cartItem = item;
                    break;
                }
            }
            return cartItem;
        }

        public Cart RemoveItem(CartItem cartItem) {
            List<CartItem> cartItems = new List<CartItem>();
            if (cartItem != null)
            {
                this.Quantity = 0;
                this.Total = 0;
                foreach (var item in items)
                {
                    if (item.BookID != cartItem.BookID ) {
                        cartItems.Add(item);
                        this.Quantity += item.Quantity;
                        this.Total += item.Total;
                    }
                }
                this.items = cartItems;                  
            }

            return this;
        }

        public Cart EmptyItems() {
            items = new List<CartItem>();            
            this.Total = 0;
            this.Quantity = 0;
            return this;
        }

        public Cart UpdateQuantity(int bookID, int quantity ) {
            
            foreach (var item in items)
            {
                if (item.BookID == bookID )
                {
                   // this.Quantity -= item.Quantity + quantity;
                    if (this.Quantity > quantity) {
                        this.Quantity = this.Quantity - quantity;
                    }
                    this.Total -= item.Total;
                    item.Quantity = quantity;
                    item.Total = item.Quantity * item.Price;
                    this.Total += item.Total;
                    item.UpdateQuantity(item.Quantity);
                    
                    break;
                }
            }
            return this;
        }

    }
}
